<?php
/**
 * @author spenceryg
 * @date 2022/11/25
 * @time 10:54 下午
 * @focus love & life
 */

namespace app\admin\controller\sys\business;

use app\admin\controller\sys\model\AdminRoleAccessModel;
use app\admin\controller\sys\model\AdminRoleModel;
use app\admin\controller\sys\model\AdminRoleNodeModel;
use app\admin\controller\sys\validate\AdminRoleValidate;

class AdminRoleBusiness
{
    public static function add($data)
    {
        validate(AdminRoleValidate::class)->check($data);
        $node_arr = $data['node'];
        $access_arr = $data['access'];
        $admin_role = AdminRoleModel::create($data);
        $id = $admin_role->id;

        $save = [];
        foreach ($node_arr as $node) {
            $save = [];
            $save['admin_role_id'] = $id;
            $save['admin_node_id'] = $node;
            AdminRoleNodeModel::create($save);
        }
        foreach ($access_arr as $access) {
            $save = [];
            $save['admin_role_id'] = $id;
            $save['admin_access_id'] = $access;
            AdminRoleAccessModel::create($save);
        }
        return $id;
    }


    public function update($data)
    {
        validate(AdminRoleValidate::class)->check($data);
        $node_arr = $data['node'];
        $access_arr = $data['access'];
        AdminRoleModel::update($data);

        AdminRoleNodeModel::where('admin_role_id', $data['id'])->delete();
        AdminRoleAccessModel::where('admin_role_id', $data['id'])->delete();
        $save = [];
        foreach ($node_arr as $node) {
            $save = [];
            $save['admin_role_id'] = $data['id'];
            $save['admin_node_id'] = $node;
            AdminRoleNodeModel::create($save);
        }
        foreach ($access_arr as $access) {
            $save = [];
            $save['admin_role_id'] = $data['id'];
            $save['admin_access_id'] = $access;
            AdminRoleAccessModel::create($save);
        }
    }
}
